package com.dooboolab.fluttersound;

import androidx.annotation.NonNull;

import com.dooboolab.TauEngine.Flauto;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.PluginRegistry.Registrar;

public class FlutterSound
        implements FlutterPlugin,
        ActivityAware {
    FlutterPlugin.FlutterPluginBinding pluginBinding;

    @Override
    public void onAttachedToEngine(FlutterPlugin.FlutterPluginBinding binding) {
        this.pluginBinding = binding;
    }

    /**
     * Plugin registration.
     */
    public static void registerWith(Registrar registrar) {
        if (registrar.activity() == null) {
            return;
        }
        //reg = registrar;
        Flauto.androidContext = registrar.context();
        Flauto.androidActivity = registrar.activity();

        FlutterSoundPlayerManager.attachFlautoPlayer(Flauto.androidContext, registrar.messenger());
        FlutterSoundRecorderManager.attachFlautoRecorder(Flauto.androidContext, registrar.messenger());
    }


    @Override
    public void onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding) {
    }

    @Override
    public void onDetachedFromActivity() {
    }

    @Override
    public void onReattachedToActivityForConfigChanges(
            @NonNull
            ActivityPluginBinding binding
    ) {

    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {

    }

    @Override
    public void onAttachedToActivity(
            @NonNull
            ActivityPluginBinding binding
    ) {
        Flauto.androidActivity = binding.getActivity();

        // Only register if activity exists (the application is not running in background)
        Flauto.androidContext = pluginBinding.getApplicationContext();
        FlutterSoundPlayerManager.attachFlautoPlayer(Flauto.androidContext, pluginBinding.getBinaryMessenger());
        FlutterSoundRecorderManager.attachFlautoRecorder(Flauto.androidContext, pluginBinding.getBinaryMessenger());
    }


}
